import json

class GlobalStorage:
    """
    全局存储模块
    """
    _instance = None
    # storage = {"age":18}
    storage = None
    def __new__(cls, *args, **kwargs):
        print("GlobalStorage new")
        if not cls._instance:
            cls._instance = super(GlobalStorage, cls).__new__(cls, *args, **kwargs)
        return cls._instance

    def __init__(self):
        super().__init__()
        self.storage = {}

    def set(self, key, value):
        """
        设置全局变量
        """
        print("===========")
        print(self.storage.get("age"))
        print("===========")
        self.storage[key] = value

    def get(self, key):
        """
        获取全局变量
        """
        return self.storage.get(key)

    def dump(self):
        """
        将全局变量序列化
        """
        return json.dumps(self.storage)

    def load(self, data):
        """
        从序列化数据中加载全局变量
        """
        self.storage = json.loads(data)

    def dump(self):
        """
        将全局变量序列化
        """
        return json.dumps(self.storage)

    def load(self, data):
        """
        从序列化数据中加载全局变量
        """
        self.storage = json.loads(data)

globalSingle = GlobalStorage()